/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <devmon/driver_abi.h>
#include <dlfcn.h>
#include <gtest/gtest.h>
#include <test_utilities/test_base.h>

#include "../../../drivers/bus/i2c/i2c.h"
#include "../../../drivers/chip/eeprom/eeprom.h"
#include "../../../drivers/internal/hasdef.h"
#include "../../test_utils/root_object.h"

using namespace dev;
using namespace devmon;

namespace drivers {
namespace chip {
namespace eeprom_test {

class eeprom_test_service : public mc::engine::service {
public:
    eeprom_test_service(const std::string& name) : mc::engine::service(name) {
    }
};

class EepromAbiTest : public mc::test::TestWithEngine {
public:
    static void SetUpTestSuite() {
        TestWithEngine::SetUpTestSuite();
        // 动态创建服务实例，避免全局静态初始化导致的析构顺序问题
        m_test_service = mc::make_shared<eeprom_test_service>("bmc.kepler.test_eeprom");
        m_test_service->init();
        m_test_service->start();

        // 创建根对象
        m_root_object = root_object::create_root_object(m_test_service.get());

        load_driver();
    }

    static void TearDownTestSuite() {
        m_test_service->stop();
        m_root_object.reset();
        
        // 清理设备容器，避免引用已关闭的动态库
        m_devices.clear();
        m_i2c_devices.clear();

        // 不手动dlclose，避免静态对象析构时访问已卸载的动态库导致SIGSEGV
        // 让系统在程序退出时自动卸载动态库
        m_test_service.reset();
        TestWithEngine::TearDownTestSuite();
    }

    void SetUp() override {
        // 底层驱动打桩会自动管理内存，无需手动清理
    }

    void TearDown() override {
        // 底层驱动打桩会自动管理内存，无需手动清理
    }

    static void load_driver();

    // 创建并初始化总线驱动
    std::shared_ptr<device_driver_t> create_bus_driver(const std::string& driver_name);

    mc::dict create_test_connector(uint8_t system_id = 1, uint8_t position = 1, uint8_t slot = 1);

    // 创建总线CSR对象
    mc::dict create_bus_csr_object(uint8_t bus_id, uint32_t bus_speed, uint8_t bus_mode,
                                   uint8_t bus_slave_addr, bool bus_use_smbus);

    // 创建EEPROM芯片CSR对象
    mc::dict create_eeprom_csr_object(uint32_t address, uint8_t offset_width, uint8_t address_width,
                                      uint32_t write_timeout, uint32_t read_timeout,
                                      uint8_t rw_block_size, uint8_t write_interval,
                                      uint8_t read_interval, uint8_t write_retry, 
                                      uint8_t read_retry);

    static mc::shared_ptr<eeprom_test_service>                              m_test_service;
    static std::unordered_map<std::string, std::shared_ptr<device_driver_t>> m_devices;
    static void*                                                             m_driver_handle;
    static void*                                                             m_i2c_driver_handle;
    static std::unordered_map<std::string, std::shared_ptr<device_driver_t>> m_i2c_devices;
    static root_object_ptr                                                   m_root_object;
};

} // namespace eeprom_test
} // namespace chip
} // namespace drivers

// 静态变量定义
using namespace drivers::chip::eeprom_test;

mc::shared_ptr<eeprom_test_service> EepromAbiTest::m_test_service;

std::unordered_map<std::string, std::shared_ptr<device_driver_t>> EepromAbiTest::m_devices;

void* EepromAbiTest::m_driver_handle = nullptr;
void* EepromAbiTest::m_i2c_driver_handle = nullptr;

std::unordered_map<std::string, std::shared_ptr<device_driver_t>> EepromAbiTest::m_i2c_devices;

root_object_ptr EepromAbiTest::m_root_object;

// 方法实现
void EepromAbiTest::load_driver() {
    // 加载EEPROM驱动
    m_driver_handle = dlopen("./opt/bmc/drivers/libEeprom.so", RTLD_LAZY);
    if (m_driver_handle == nullptr) {
        MC_THROW(mc::system_exception, "Failed to load Eeprom driver: ${error}",
                 ("error", dlerror()));
    }
    register_device_driver_func func = reinterpret_cast<register_device_driver_func>(
        dlsym(m_driver_handle, "register_device_driver"));
    MC_ASSERT_THROW(func, mc::bad_function_call_exception,
                    "Failed to get register_device_driver function: ${error}",
                    ("error", dlerror()));

    device_driver_t* devices = nullptr;
    uint8_t devices_count = 0;
    status_t ret = func(&devices, &devices_count);
    MC_ASSERT_THROW(ret == STATUS_OK, mc::bad_function_call_exception,
                    "Failed to get device driver manager: ${error}", ("error", ret));

    m_devices.clear();
    m_devices.reserve(devices_count);
    for (uint8_t i = 0; i < devices_count; i++) {
        auto device_driver = std::make_shared<device_driver_t>(devices[i]);
        m_devices.emplace(device_driver->device_name, device_driver);
    }

    // 加载I2C驱动
    m_i2c_driver_handle = dlopen("./opt/bmc/drivers/libI2c.so", RTLD_LAZY);
    if (m_i2c_driver_handle == nullptr) {
        MC_THROW(mc::system_exception, "Failed to load I2c driver: ${error}",
                 ("error", dlerror()));
    }
    register_device_driver_func i2c_func = reinterpret_cast<register_device_driver_func>(
        dlsym(m_i2c_driver_handle, "register_device_driver"));
    MC_ASSERT_THROW(i2c_func, mc::bad_function_call_exception,
                    "Failed to get I2c register_device_driver function: ${error}",
                    ("error", dlerror()));

    device_driver_t* i2c_devices = nullptr;
    uint8_t i2c_devices_count = 0;
    status_t i2c_ret = i2c_func(&i2c_devices, &i2c_devices_count);
    MC_ASSERT_THROW(i2c_ret == STATUS_OK, mc::bad_function_call_exception,
                    "Failed to get I2c device driver manager: ${error}", ("error", i2c_ret));

    m_i2c_devices.clear();
    m_i2c_devices.reserve(i2c_devices_count);
    for (uint8_t i = 0; i < i2c_devices_count; i++) {
        auto device_driver = std::make_shared<device_driver_t>(i2c_devices[i]);
        m_i2c_devices.emplace(device_driver->device_name, device_driver);
    }
}

std::shared_ptr<device_driver_t> 
EepromAbiTest::create_bus_driver(const std::string& driver_name) {
    if (driver_name == "I2c") {
        auto it = m_i2c_devices.find("I2c");
        if (it != m_i2c_devices.end()) {
            return it->second;
        }
    }
    return nullptr;
}

mc::dict EepromAbiTest::create_test_connector(
    uint8_t system_id, uint8_t position, uint8_t slot) {
    return mc::dict{{"SystemId", system_id}, {"Position", position}, {"Slot", slot}};
}

mc::dict EepromAbiTest::create_bus_csr_object(
    uint8_t bus_id, uint32_t bus_speed, uint8_t bus_mode,
    uint8_t bus_slave_addr, bool bus_use_smbus) {
    return mc::dict{{"Id", bus_id},
                    {"Speed", bus_speed},
                    {"Mode", bus_mode},
                    {"SlaveAddr", bus_slave_addr},
                    {"UseSmbus", bus_use_smbus}};
}

mc::dict EepromAbiTest::create_eeprom_csr_object(
    uint32_t address, uint8_t offset_width, uint8_t address_width,
    uint32_t write_timeout, uint32_t read_timeout,
    uint8_t rw_block_size, uint8_t write_interval,
    uint8_t read_interval, uint8_t write_retry, 
    uint8_t read_retry) {
    return mc::dict{{"Address", address},
                    {"OffsetWidth", offset_width},
                    {"AddrWidth", address_width},
                    {"WriteTmout", write_timeout},
                    {"ReadTmout", read_timeout},
                    {"RwBlockSize", rw_block_size},
                    {"WriteInterval", write_interval},
                    {"ReadInterval", read_interval},
                    {"WriteRetryTimes", write_retry},
                    {"ReadRetryTimes", read_retry}};
}

namespace drivers {
namespace chip {
namespace eeprom_test {

// ========================= 驱动注册功能测试 =========================

TEST_F(EepromAbiTest, register_device_driver_success) {
    EXPECT_EQ(m_devices.size(), 1);
    EXPECT_TRUE(m_devices.find("Eeprom") != m_devices.end());

    // 验证I2C驱动也正确加载
    EXPECT_EQ(m_i2c_devices.size(), 1);
    EXPECT_TRUE(m_i2c_devices.find("I2c") != m_i2c_devices.end());
}

TEST_F(EepromAbiTest, device_driver_structure_validation) {
    for (const auto& [device_name, device_driver] : m_devices) {
        EXPECT_FALSE(std::string(device_driver->device_name).empty());
        EXPECT_TRUE(device_driver->ctor != nullptr);
        EXPECT_TRUE(device_driver->init != nullptr);
        EXPECT_TRUE(device_driver->start != nullptr);
        EXPECT_TRUE(device_driver->stop != nullptr);
    }
}

// ========================= EEPROM设备创建测试 =========================
TEST_F(EepromAbiTest, create_eeprom_success) {
    // 使用唯一object_name创建内部对象
    std::string object_name = "test_eeprom_device_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);

    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto device = device_driver->ctor(m_test_service.get(), object_name.c_str());
    ASSERT_TRUE(device != nullptr);
    
    // 设置父对象后再注册
    auto eeprom_obj = static_cast<dev::eeprom_object*>(device);
    eeprom_obj->set_parent(m_root_object.get());
    m_test_service->register_object(static_cast<mc::engine::object_impl*>(device));
}

TEST_F(EepromAbiTest, create_eeprom_null_service) {
    std::string object_name = "test_eeprom_null_service_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);

    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto device = device_driver->ctor(nullptr, object_name.c_str());
    EXPECT_EQ(device, nullptr);
}

TEST_F(EepromAbiTest, create_eeprom_null_name) {
    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto device = device_driver->ctor(m_test_service.get(), nullptr);
    EXPECT_EQ(device, nullptr);
}

// ========================= EEPROM设备初始化测试 =========================
TEST_F(EepromAbiTest, init_eeprom_success) {
    // 先创建前级i2c内部对象
    std::string i2c_object_name = "test_i2c_for_eeprom";
    mc::mutable_dict i2c_csr = create_bus_csr_object(0, 3400, 0, 0x50, false);
    i2c_csr["object_name"] = i2c_object_name;
    auto i2c_internal = dev::device_manager::get_instance().create_bus(i2c_object_name, "I2c", i2c_csr);
    ASSERT_TRUE(i2c_internal != nullptr);
    
    // 创建eeprom内部对象
    std::string eeprom_object_name = "test_eeprom_init_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = eeprom_object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(eeprom_object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);

    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto device = device_driver->ctor(m_test_service.get(), eeprom_object_name.c_str());
    ASSERT_TRUE(device != nullptr);
    auto connector  = create_test_connector(1, 1, 1);
    status_t ret = device_driver->init(device, &csr_object, &connector);
    EXPECT_EQ(ret, STATUS_OK);
    
    // 设置父对象后再注册
    auto eeprom_obj = static_cast<dev::eeprom_object*>(device);
    eeprom_obj->set_parent(m_root_object.get());
    m_test_service->register_object(static_cast<mc::engine::object_impl*>(device));
}

TEST_F(EepromAbiTest, init_eeprom_null_params) {
    std::string object_name = "test_eeprom_null_params_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);
    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto device = device_driver->ctor(m_test_service.get(), object_name.c_str());
    ASSERT_TRUE(device != nullptr);
    status_t ret = device_driver->init(device, nullptr, nullptr);
    EXPECT_EQ(ret, STATUS_ERROR);
}

TEST_F(EepromAbiTest, init_eeprom_null_device) {
    std::string object_name = "test_eeprom_null_device_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);
    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto connector  = create_test_connector(1, 1, 1);
    status_t ret = device_driver->init(nullptr, &csr_object, &connector);
    EXPECT_EQ(ret, STATUS_ERROR);
}

// ========================= EEPROM属性验证测试 =========================
TEST_F(EepromAbiTest, eeprom_properties_validation) {
    // 先创建前级i2c内部对象
    std::string i2c_object_name = "test_i2c_properties";
    mc::mutable_dict i2c_csr = create_bus_csr_object(0, 3400, 0, 0x50, false);
    i2c_csr["object_name"] = i2c_object_name;
    auto i2c_internal = dev::device_manager::get_instance().create_bus(i2c_object_name, "I2c", i2c_csr);
    ASSERT_TRUE(i2c_internal != nullptr);

    // 创建eeprom内部对象
    std::string eeprom_object_name = "test_eeprom_properties_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 2, 1, 2000, 1500, 64, 100, 20, 5, 3);
    csr_object["object_name"] = eeprom_object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(eeprom_object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);

    auto device_driver = m_devices["Eeprom"];
    ASSERT_TRUE(device_driver != nullptr);
    auto device = device_driver->ctor(m_test_service.get(), eeprom_object_name.c_str());
    ASSERT_TRUE(device != nullptr);
    auto connector = create_test_connector(1, 1, 1);
    status_t init_ret = device_driver->init(device, &csr_object, &connector);
    ASSERT_EQ(init_ret, STATUS_OK);

    // 设置父对象后再注册
    auto eeprom_obj = static_cast<dev::eeprom_object*>(device);
    eeprom_obj->set_parent(m_root_object.get());
    m_test_service->register_object(static_cast<mc::engine::object_impl*>(device));

    // 验证interface_chip属性值
    EXPECT_EQ(eeprom_obj->m_interface_chip.Address.get_value(), 0x50);
    EXPECT_EQ(eeprom_obj->m_interface_chip.OffsetWidth.get_value(), 2);
    EXPECT_EQ(eeprom_obj->m_interface_chip.AddrWidth.get_value(), 1);
    EXPECT_EQ(eeprom_obj->m_interface_chip.WriteTmout.get_value(), 2000);
    EXPECT_EQ(eeprom_obj->m_interface_chip.ReadTmout.get_value(), 1500);
    EXPECT_EQ(eeprom_obj->m_interface_chip.RwBlockSize.get_value(), 64);
    EXPECT_EQ(eeprom_obj->m_interface_chip.WriteInterval.get_value(), 100);
    EXPECT_EQ(eeprom_obj->m_interface_chip.ReadInterval.get_value(), 20);
    EXPECT_EQ(eeprom_obj->m_interface_chip.WriteRetryTimes.get_value(), 5);
    EXPECT_EQ(eeprom_obj->m_interface_chip.ReadRetryTimes.get_value(), 3);
}

// ========================= EEPROM功能测试 =========================
TEST_F(EepromAbiTest, block_read_write) {
    // 先创建前级i2c内部对象
    std::string i2c_object_name = "test_i2c_rw";
    mc::mutable_dict i2c_csr = create_bus_csr_object(0, 3400, 0, 0x50, false);
    i2c_csr["object_name"] = i2c_object_name;
    auto i2c_internal = dev::device_manager::get_instance().create_bus(i2c_object_name, "I2c", i2c_csr);
    ASSERT_TRUE(i2c_internal != nullptr);

    // 创建eeprom内部对象
    std::string eeprom_object_name = "test_eeprom_rw_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = eeprom_object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(eeprom_object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);

    auto eeprom_driver = m_devices["Eeprom"];
    ASSERT_TRUE(eeprom_driver != nullptr);
    auto eeprom_device = eeprom_driver->ctor(m_test_service.get(), eeprom_object_name.c_str());
    ASSERT_TRUE(eeprom_device != nullptr);
    auto eeprom_obj = static_cast<dev::eeprom_object*>(eeprom_device);

    auto i2c_driver = create_bus_driver("I2c");
    ASSERT_TRUE(i2c_driver != nullptr);
    auto i2c_device = i2c_driver->ctor(m_test_service.get(), i2c_object_name.c_str());
    ASSERT_TRUE(i2c_device != nullptr);
    auto i2c_obj = static_cast<dev::I2c_object*>(i2c_device);

    auto bus_connector = create_test_connector(1, 1, 1);
    auto chip_connector = create_test_connector(1, 1, 1);

    // 初始化I2C总线
    status_t ret = i2c_driver->init(i2c_device, &i2c_csr, &bus_connector);
    ASSERT_EQ(ret, STATUS_OK);

    // 初始化EEPROM芯片
    ret = eeprom_driver->init(eeprom_device, &csr_object, &chip_connector);
    ASSERT_EQ(ret, STATUS_OK);

    // 设置父对象
    i2c_obj->set_parent(m_root_object.get());
    eeprom_obj->set_parent(i2c_obj);

    // 注册对象到服务
    m_test_service->register_object(eeprom_obj);
    m_test_service->register_object(i2c_obj);

    // 设置连接关系
    eeprom_internal->set_left_bus(i2c_internal);
    eeprom_internal->set_host_bus(i2c_internal);

    // 测试块读写功能
    std::string write_data = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C";
    std::vector<uint8_t> write_data_vec(write_data.begin(), write_data.end());

    // 通过资源树对象的BlockIOWrite方法进行块写入
    eeprom_obj->m_interface_chip.BlockIOWrite(0x10, write_data_vec);

    // 通过资源树对象的BlockIORead方法进行块读取
    std::vector<uint8_t> read_buffer = eeprom_obj->m_interface_chip.BlockIORead(0x10, write_data.length());
    std::string read_result_str(read_buffer.begin(), read_buffer.end());
    ASSERT_EQ(read_result_str, write_data);
}

TEST_F(EepromAbiTest, cross_page_read_write) {
    // 先创建前级i2c内部对象
    std::string i2c_object_name = "test_i2c_cross_page";
    mc::mutable_dict i2c_csr = create_bus_csr_object(0, 3400, 0, 0x50, false);
    i2c_csr["object_name"] = i2c_object_name;
    auto i2c_internal = dev::device_manager::get_instance().create_bus(i2c_object_name, "I2c", i2c_csr);
    ASSERT_TRUE(i2c_internal != nullptr);

    // 创建eeprom内部对象，设置32字节页大小
    std::string eeprom_object_name = "test_eeprom_cross_page_obj";
    mc::mutable_dict csr_object = create_eeprom_csr_object(0x50, 1, 1, 1000, 1000, 32, 50, 10, 3, 3);
    csr_object["object_name"] = eeprom_object_name;
    auto eeprom_internal = dev::device_manager::get_instance().create_chip(eeprom_object_name, "Eeprom", csr_object);
    ASSERT_TRUE(eeprom_internal != nullptr);

    auto eeprom_driver = m_devices["Eeprom"];
    ASSERT_TRUE(eeprom_driver != nullptr);
    auto eeprom_device = eeprom_driver->ctor(m_test_service.get(), eeprom_object_name.c_str());
    ASSERT_TRUE(eeprom_device != nullptr);
    auto eeprom_obj = static_cast<dev::eeprom_object*>(eeprom_device);

    auto i2c_driver = create_bus_driver("I2c");
    ASSERT_TRUE(i2c_driver != nullptr);
    auto i2c_device = i2c_driver->ctor(m_test_service.get(), i2c_object_name.c_str());
    ASSERT_TRUE(i2c_device != nullptr);
    auto i2c_obj = static_cast<dev::I2c_object*>(i2c_device);

    auto bus_connector = create_test_connector(1, 1, 1);
    auto chip_connector = create_test_connector(1, 1, 1);

    // 初始化I2C总线
    status_t ret = i2c_driver->init(i2c_device, &i2c_csr, &bus_connector);
    ASSERT_EQ(ret, STATUS_OK);

    // 初始化EEPROM芯片
    ret = eeprom_driver->init(eeprom_device, &csr_object, &chip_connector);
    ASSERT_EQ(ret, STATUS_OK);

    // 设置父对象
    i2c_obj->set_parent(m_root_object.get());
    eeprom_obj->set_parent(i2c_obj);

    // 注册对象到服务
    m_test_service->register_object(eeprom_obj);
    m_test_service->register_object(i2c_obj);

    // 设置连接关系
    eeprom_internal->set_left_bus(i2c_internal);
    eeprom_internal->set_host_bus(i2c_internal);

    // 测试跨页读写功能
    // 页大小为32字节，从偏移量30开始写入10字节数据，这样会跨越第一页边界
    uint32_t cross_page_offset = 30;  // 在第一页的最后2字节开始写入
    std::string cross_page_data = "\xAA\xBB\xCC\xDD\xEE\xFF\x11\x22\x33\x44";  // 10字节数据
    std::vector<uint8_t> cross_page_vec(cross_page_data.begin(), cross_page_data.end());

    // 通过资源树对象的BlockIOWrite方法进行跨页块写入
    eeprom_obj->m_interface_chip.BlockIOWrite(cross_page_offset, cross_page_vec);

    // 通过资源树对象的BlockIORead方法进行跨页块读取
    std::vector<uint8_t> read_buffer = eeprom_obj->m_interface_chip.BlockIORead(cross_page_offset, cross_page_data.length());
    std::string read_result_str(read_buffer.begin(), read_buffer.end());
    ASSERT_EQ(read_result_str, cross_page_data) << "Cross-page read/write data should match";

    // 测试另一个跨页场景：从页中间开始，写入跨越多页的大数据块
    uint32_t multi_page_offset = 20;  // 从第一页中间开始
    std::string multi_page_data;
    // 创建60字节的测试数据，将跨越页边界（页大小32字节）
    for (int i = 0; i < 60; i++) {
        multi_page_data.push_back(static_cast<char>(0x50 + (i % 16)));
    }
    std::vector<uint8_t> multi_page_vec(multi_page_data.begin(), multi_page_data.end());

    // 写入跨多页数据
    eeprom_obj->m_interface_chip.BlockIOWrite(multi_page_offset, multi_page_vec);

    // 读取跨多页数据
    std::vector<uint8_t> multi_read_buffer = eeprom_obj->m_interface_chip.BlockIORead(multi_page_offset, multi_page_data.length());
    std::string multi_read_result(multi_read_buffer.begin(), multi_read_buffer.end());
    ASSERT_EQ(multi_read_result, multi_page_data) << "Multi-page read/write data should match";

    // 测试页边界对齐的写入
    uint32_t aligned_offset = 64;  // 第三页开始（64 = 32 * 2）
    std::string aligned_data = "\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00\x11\x22\x33\x44\x55\x66\x77";  // 16字节
    std::vector<uint8_t> aligned_vec(aligned_data.begin(), aligned_data.end());

    // 页对齐写入
    eeprom_obj->m_interface_chip.BlockIOWrite(aligned_offset, aligned_vec);

    // 页对齐读取
    std::vector<uint8_t> aligned_read_buffer = eeprom_obj->m_interface_chip.BlockIORead(aligned_offset, aligned_data.length());
    std::string aligned_read_result(aligned_read_buffer.begin(), aligned_read_buffer.end());
    ASSERT_EQ(aligned_read_result, aligned_data) << "Page-aligned read/write data should match";
}

} // namespace eeprom_test
} // namespace chip
} // namespace drivers 